
package xylophone;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.layout.Pane;
import javafx.scene.media.AudioClip;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 * A sample that demonstrates the basics of AudioClips.
 */
public final class Xylophone extends Application {
    private void init(Stage primaryStage) {
        Group root = new Group();
        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(root, 480, 350));

        AudioClip[] barNotes = new AudioClip[8];

        for (int i = 0; i < barNotes.length; i++)
            barNotes[i] = new AudioClip(Xylophone.class.getResource("Note" + (i + 1) + ".wav").toString());

        Group rectangleGroup = new Group();

        double xStart = 50.0;
        double xOffset = 30.0;
        double yPos = 180.0;
        double barWidth = 22.0;

        Color[] cs = {Color.PURPLE, Color.BLUEVIOLET, Color.BLUE, Color.GREEN, Color.GREENYELLOW, Color.YELLOW, Color.ORANGE, Color.RED};


        Light.Point light = new Light.Point();
        light.setX(-20);
        light.setY(-20);
        light.setZ(100);

        Lighting l = new Lighting();
        l.setLight(light);
        l.setSurfaceScale(1.0f);

        Group[] baseGroups = new Group[2];
        for (int i = 0; i < baseGroups.length; i++) {
            baseGroups[i] = createRectangle(new Color(0.2, 0.12, 0.1, 1.0),
                    xStart + 135, yPos + 20 - 40 * i, barWidth * 11.5, 10);
            rectangleGroup.getChildren().add(baseGroups[i]);
        }

        Group[] barGroups = new Group[8];
        for (int i = 0; i < barGroups.length; i++) {
            barGroups[i] = createRectangle(cs[i], xStart + (i + 1) * xOffset, yPos, barWidth, 100 - 5 * i);
            final int j = i;
            barGroups[i].setOnMousePressed(e -> barNotes[j].play());
            barGroups[i].setEffect(l);
            rectangleGroup.getChildren().add(barGroups[i]);
        }


        rectangleGroup.setScaleX(1.8);
        rectangleGroup.setScaleY(1.8);
        rectangleGroup.setTranslateX(55.0);

        Pane pane = new Pane();
        pane.getChildren().add(rectangleGroup);

        root.getChildren().add(pane);

    }

    private static Group createRectangle(Color color, double tx, double ty, double sx, double sy) {
        Group squareGroup = new Group();
        Rectangle squareShape = new Rectangle(1.0, 1.0);
        squareShape.setFill(color);
        squareShape.setTranslateX(-0.5);
        squareShape.setTranslateY(-0.5);
        squareGroup.getChildren().add(squareShape);
        squareGroup.setTranslateX(tx);
        squareGroup.setTranslateY(ty);
        squareGroup.setScaleX(sx);
        squareGroup.setScaleY(sy);
        return squareGroup;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        init(primaryStage);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
